package com.tianyi.sfzy1.controller;

import com.tianyi.sfzy1.domain.Course;
import com.tianyi.sfzy1.domain.Coursepurchaserecord;
import com.tianyi.sfzy1.service.CoursePurchaseRecordService;
import com.tianyi.sfzy1.service.CourseService;
import com.tianyi.sfzy1.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * DYT
 */

@RestController
@RequestMapping("/coursepurchase")
public class CoursePurchaseRecordController {

    @Autowired
    private CoursePurchaseRecordService coursePurchaseRecordService;

    /**
     *查询所有课程购买记录
     */
    @GetMapping
    public R findAll(String title,String status,String realname){
        System.out.println();
        List<Coursepurchaserecord> list=coursePurchaseRecordService.findAll(title,status,realname);
        return R.ok(list);
    }

    /**
     *添加新购买记录
     */
    @PostMapping
    public R insert(@RequestBody Coursepurchaserecord coursepurchaserecord){
        System.out.println(coursepurchaserecord);
        int i=coursePurchaseRecordService.insert(coursepurchaserecord);
        if (i>0){
            return R.ok(null);
        }
        else {
            return R.fail("添加失败");
        }
    }

    /**
     *小程序根据id添加新购买记录
     */
    @PostMapping("/one")
    public R insertOne(int memberId,int courseId,Double purchasePrice){
        int i=coursePurchaseRecordService.insertOne(memberId,courseId,purchasePrice);
        if (i>0){
            return R.ok(null);
        }else{
            return R.fail("添加失败");
        }
    }


    /**
     *修改课程购买记录
     */
    @PutMapping
    public R update(@RequestBody Coursepurchaserecord coursepurchaserecord){
        System.out.println(coursepurchaserecord);
        System.out.println(coursepurchaserecord);
        int i=coursePurchaseRecordService.update(coursepurchaserecord);
        if (i>0){
            return R.ok(null);
        }
        else {
            return R.fail("修改失败");
        }
    }

    /**
     *删除课程购买记录
     */
    @DeleteMapping
    public R delete(@RequestParam("purchaseId") int purchaseId){
        int i=coursePurchaseRecordService.delete(purchaseId);
        if (i>0){
            return R.ok(null);
        }
        else {
            return R.fail("删除失败");
        }
    }

    /**
     *批量删除课程购买记录
     */
    @DeleteMapping("/select")
    public R deleteSelect(@RequestBody List<Integer> list){
        int i=coursePurchaseRecordService.deleteSelect(list);
        if (i>0){
            return R.ok(null);
        }
        else {
            return R.fail("删除失败");
        }
    }

    /**
     * 小程序根据用户和课程id查询购买记录
     */
    @GetMapping("/find")
    public R find(int memberId,int courseId){
        System.out.println(memberId);
        System.out.println(courseId);
        Coursepurchaserecord coursepurchaserecord=coursePurchaseRecordService.find(memberId, courseId);
        return R.ok(coursepurchaserecord);
    }
}
